package pers.ljf.escloud.controller.app;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.ljf.escloud.common.dto.SettingDTO;
import pers.ljf.escloud.common.enums.SettingsEnum;
import pers.ljf.escloud.common.utils.BinaryUtils;
import pers.ljf.escloud.dao.entity.app.Enterprise;
import pers.ljf.escloud.service.dto.Result;
import pers.ljf.escloud.service.modules.app.EnterpriseService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/app/ent/settings")
public class SettingsController {

    @Resource
    private EnterpriseService enterpriseService;

    @GetMapping("/info/{entId}")
    @ResponseBody
    public Result settingsList(@PathVariable("entId") Long entId) {
        Enterprise enterprise = enterpriseService.getById(entId);
        Long settings = enterprise.getSettings();
        List<SettingDTO> settingDTOList = new ArrayList<>();
        for (SettingsEnum setting : SettingsEnum.values()) {
            Integer value = setting.getValue();
            String name = setting.name();
            SettingDTO settingDTO = new SettingDTO();
            settingDTO.setSettingId(setting.getValue());
            settingDTO.setSettingName(setting.name());
            settingDTO.setEntId(entId);
            settingDTO.setEnabled(BinaryUtils.getBitByIndex(settings, setting.getValue()) == 1);
            settingDTOList.add(settingDTO.getSettingId(), settingDTO);
        }
        return Result.ok().put("settings", settingDTOList);
    }
}
